
const list = [
  {
    name: "动漫", // 0.8w
    have: 2405, 
    rest: 4503,  // 
    high: 10.08,
    low: 0, 
    estimation: 1, // 
    total_difference: 0.06,
    result: 0,
  },
  {
    name: "机器", // 3w
    have: 9897, 
    rest: 19067, //
    high: 16.35,
    low: 0,  
    estimation: -2, // 
    total_difference: 0.19,
    result: 0,
  },
  {
    name: "人工", //3.5w
    have: 11587,  
    rest: 20861, //
    high: 13.96,
    low: 0,  
    estimation: 0, // 
    total_difference: 0.04,
    result: 0,
  },
  {
    name: "华安",// 6w
    have: 39.1812, // 
    rest: 31616, // 
    high: 14.55, 
    low: 0,
    estimation: 0,// 
    total_difference: -0.24,
    result: 0,
  },
];

const handle_nx = (height, low) => {
  const range = Math.abs(height - low);
  return `${Math.ceil(range / 5)}x`;
};

/**
 * 通用阶梯卖出计算函数
 * @param {number} initialAmount - 初始总量 (默认1)
 * @param {number} percent - 当前涨幅百分比
 * @param {number} nx - 触发卖出的涨幅步长
 * @returns {number} - 总卖出量
 */
const calculate_sale = (initialAmount, percent, nx, name) => {
  const increasePercent = parseFloat(percent);
  const stepPercent = nx.slice(0, -1);

  // 核心计算逻辑
  const fullStages = Math.floor(increasePercent / stepPercent);
  const fraction = increasePercent % stepPercent;

  // 阶梯卖出后剩余量，每次剩0.75，就是0.75的n次方
  const remainingAfterFull = initialAmount * Math.pow(0.75, fullStages);
  // 完整阶梯卖出量计算
  const fullStageSold = initialAmount - remainingAfterFull;

  // 未满阶梯的线性卖出量
  const fractionalRatio = fraction / stepPercent;
  const fractionalSold = remainingAfterFull * 0.25 * fractionalRatio;
  if (name === "华安") {
    return (fullStageSold + fractionalSold).toFixed(4);
  } else {
    return Math.ceil(fullStageSold + fractionalSold);
  }
};

const result_formatter = (estimation, name, nx, result) => {
  return estimation > 0
    ? `${name}_${nx}: 卖出 ${result} 份`
    : `${name}_${nx}: 买入 ${Math.ceil(result)} 元`;
};

const submit = (obj) => {
  let {
    name,
    have,
    rest,
    high,
    low,
    estimation,
    total_difference,
    result,
  } = obj;
  const total = estimation > 0 ? have : rest;
  const current_estimation =
    parseFloat(estimation) 
  const final_estimation = Math.abs(current_estimation);
  const nx = handle_nx(high, low);

  result = calculate_sale(total, final_estimation, nx, name);

  console.log(result_formatter(estimation, name, nx, result));
};

list.forEach((item) => {
  submit(item);
});
